package com.example.weathersuggestion;

import java.util.ArrayList;

import com.slidingmenu.lib.SlidingMenu;

import android.location.Location;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class LocationActivity extends Activity {
	private CustomAdapter dataAdapter = null;
	private String locationName = "";
	private String[] menuItems = {"Home","Locations"};//https://github.com/jfeinstein10/SlidingMenu
	private EditText locationTextBox = null;
	private Button addLocationButton = null;
	private String[] locationArray;
	private String locationText ="";
	private LocationList locList;
	private int tempIndex;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_locationactivity);
		locList = new LocationList(this);
	//	locationArray = locList.getLocationListTitles();
		bindObjects();
		setupSlidingMenu();
		//Generate list View from ArrayList
		displayListView();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.location, menu);
		return true;
	}
	
	private void bindObjects(){
		locationText ="";
		addLocationButton = (Button) findViewById(R.id.addLocationButton);
		locationTextBox = (EditText) findViewById(R.id.locationAddTextView);
		//removeLocationButton = (Button) findViewById(R.id.removeLocationButton);
		
		addLocationButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				locationText = locationTextBox.getText().toString();
				Location temp = locList.addNewLocation(locationText);
				
				if(temp!=null){ //location successfully added
					Toast.makeText(getBaseContext(), "Location Added",Toast.LENGTH_LONG).show();
				}
				else{ //failed to add 
					Toast.makeText(getBaseContext(), "Add Failed", Toast.LENGTH_LONG).show();
				}
			}
		});
		/**Not sure if we want to use this to remove location or the long press
		removeLocationButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				locationText = locationTextBox.getText().toString();
				Location temp = locList.addNewLocation(locationText);
				
				if(temp!=null){ //location successfully added
					Toast.makeText(getParent(), "Location Added",Toast.LENGTH_LONG).show();
				}
				else{ //failed to add 
					Toast.makeText(getParent(), "Add Failed", Toast.LENGTH_LONG).show();
				}
			}
		});
		**/

		
	}

	private void displayListView() {
		  
		  String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
			        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
			        "Linux", "OS/2" };
		  
		  //call method to build location array from preferences file
		  
		  
			    ArrayAdapter<String> adapter = new CustomAdapter(this,locationArray);
			    
			    ListView listView = (ListView) findViewById(R.id.listView1);
			    
			    listView.setAdapter(adapter);
			    listView.setOnItemLongClickListener(new OnItemLongClickListener() {

					@Override
					public boolean onItemLongClick(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
						//creates alert allowing you do delete item from listview and preferences file
						tempIndex = arg2;
						AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getBaseContext());
				    	alertDialogBuilder
				    	.setTitle("Remove Location")
				    	.setMessage("Do you really want to remove location?")
				    	.setCancelable(true)
				    	.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

				    		@Override
				    		public void onClick(DialogInterface dialog, int which) {
				    		//	locList.removeLocation(tempIndex);
				    			
				    		}
				    	})
				    	.setNegativeButton("No", new DialogInterface.OnClickListener() {

				    		@Override
				    		public void onClick(DialogInterface dialog, int which) {
				    			dialog.cancel();	
				    		}
				    	});

				    	AlertDialog alertDialog = alertDialogBuilder.create();
				    	alertDialog.show();
				    	return true;
					}
			    });
					
					
				
			    
			    listView.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
						Intent intent = null;
						tempIndex = arg2;
						AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getBaseContext());
				    	alertDialogBuilder
				    	.setTitle("Set Location")
				    	.setMessage("Do you wish to set this location?")
				    	.setCancelable(true)
				    	.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

				    		@Override
				    		public void onClick(DialogInterface dialog, int which) {
				    		//	locList.setSelectedLocation(tempIndex);
				    			
				    		}
				    	})
				    	.setNegativeButton("No", new DialogInterface.OnClickListener() {

				    		@Override
				    		public void onClick(DialogInterface dialog, int which) {
				    			dialog.cancel();	
				    		}
				    	});

				    	AlertDialog alertDialog = alertDialogBuilder.create();
				    	alertDialog.show();
					}
				});
			    
	}
	
	private void setupSlidingMenu() {
		SlidingMenu sMenu = new SlidingMenu(this);
		sMenu.setMode(SlidingMenu.LEFT);
		sMenu.setBehindOffsetRes(R.dimen.behindOff);
		sMenu.setFadeDegree(0.35f);
		sMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		sMenu.setMenu(R.layout.slide_menu);
		sMenu.setShadowWidth(R.dimen.shadowWidth);
		sMenu.setShadowDrawable(R.drawable.shadow);
		sMenu.setShadowWidthRes(R.dimen.shadowWidth);
		sMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);

		ListView lv = (ListView) findViewById(R.id.slideMenuListView); 
		 lv.setAdapter(new ArrayAdapter<String>(this,
	        		android.R.layout.simple_list_item_1, menuItems));

		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = null;
				switch (arg2)
				{
				case 0://Refresh
					//new Connection().execute();
					intent = new Intent(LocationActivity.this, MainActivity.class);
					startActivity(intent);
					break;
				case 1: //Change Location
					intent = new Intent(LocationActivity.this, LocationActivity.class);
					startActivity(intent);
					break;
					
				}
				
			}
		});	
	}
	
	public class CustomAdapter extends ArrayAdapter<String> {
		  private final Context context;
		  private final String[] values;

		  public CustomAdapter(Context context, String[] values) {
		    super(context, R.layout.listview_item, values);
		    this.context = context;
		    this.values = values;
		  }

		@Override
		  public View getView(int position, View convertView, ViewGroup parent) {
		    LayoutInflater inflater = (LayoutInflater) context
		        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		    View rowView = inflater.inflate(R.layout.listview_item, parent, false);
		    
		    CheckBox checkBox = (CheckBox) rowView.findViewById(R.id.checkBox1);
		    TextView textView = (TextView) rowView.findViewById(R.id.code);
		   
		    //textView.setText(values[position]);
		    checkBox.setTag(values[position]);
		    checkBox.setText(values[position]);
		    

		    return rowView;
		  }
		} 


		 

}
